const {
    getBrandListServe,
    addBrandServe,
    updateBrandServe,
    getBrandById,
    deleteBrandServe
} = require('../serve/brand.serve')
const {btnAuth} = require('../middleware/auth.middleware')

class BrandController {
    async getBrandList(ctx) {
        if (!(btnAuth(ctx, 'btn.Brand.add') || btnAuth(ctx, 'btn.Brand.update') || btnAuth(ctx, 'btn.Brand.remove'))) {
            return ctx.body = {
                msg: '无权限',
                status: 1
            }
        }
        try {
            const res = await getBrandListServe(ctx)
            ctx.body = {
                msg: '获取成功',
                status: 0,
                data: {
                    total: res.total,
                    pageSize: res.pageSize,
                    pageNum: res.pageNum,
                    pageData: res.data
                }
            }
        } catch (e) {
            ctx.body = {
                msg: e.message,
                status: 1
            }
        }
    }

    async addBrand(ctx) {
        if (btnAuth(ctx, 'btn.Brand.add')) {
            try {
                await addBrandServe(ctx);
                ctx.body = {
                    msg: '添加品牌成功',
                    status: 0
                }
            } catch (e) {
                console.log(e);
                ctx.body = {
                    msg: e.message,
                    status: 1
                }
            }
        } else {
            ctx.body = {
                msg: '无权限',
                status: 1
            }
        }
    }

    async updateBrand(ctx) {
        if (btnAuth(ctx, 'btn.Brand.update')) {
            const {id} = ctx.params;
            const res = await getBrandById(id);
            if (!res) {
                return ctx.body = {
                    status: 1,
                    msg: '未找到id对应数据'
                }
            }
            try {
                const isSuccessUpdate = await updateBrandServe(ctx, id)
                ctx.body = {
                    msg: '修改菜单成功',
                    status: 0,
                    data: isSuccessUpdate
                }
            } catch (e) {
                ctx.body = {
                    msg: e.message,
                    status: 1
                }
            }
        } else {
            ctx.body = {
                msg: '无权限',
                status: 1
            }
        }
    }

    async deleteBrand(ctx) {
        if (btnAuth(ctx, 'btn.Brand.remove')) {
            const {id} = ctx.params;
            if (!id) {
                return ctx.body = {
                    msg: 'id不能为空'
                }
            }
            const res = await getBrandById(id);
            if (!res) {
                return ctx.body = {
                    status: 1,
                    msg: '未找到id对应数据'
                }
            }
            try {
                const isSuccessDelete = await deleteBrandServe(id);
                ctx.body = {
                    msg: '删除菜单成功',
                    status: 0,
                    data: isSuccessDelete
                }
            } catch (e) {
                ctx.body = {
                    msg: e.message,
                    status: 1
                }
            }
        } else {
            ctx.body = {
                msg: '无权限',
                status: 1
            }
        }
    }

    async getBrandDetail(ctx) {
        const {id} = ctx.params;
        try {
            const res = await getBrandById(id)
            ctx.body = {
                data: res,
                status: 0,
                mag: '获取成功'
            }
        } catch (e) {
            ctx.body = {
                status: 0,
                mag: e.message
            }
        }
    }
}

module.exports = new BrandController();
